package javafx.com;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class main extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Button button = new Button();
		button.setText("字体");

		button.setLayoutX(100);
		button.setLayoutY(100);
		button.setPrefWidth(200);
		button.setPrefHeight(200);

		// 单击事件
		button.setOnAction((event) -> {
			System.out.println("单击事件");
		});

		// 双击事件/多击事件
		button.addEventHandler(MouseEvent.MOUSE_CLICKED, (event) -> {
			if (event.getClickCount() == 2 && event.getButton().name().equals(MouseButton.PRIMARY.name())) {
				System.out.println("鼠标按键 = " + event.getButton().name());
				System.out.println("双击事件");
			}
		});

//		button.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
//
//			@Override
//			public void handle(MouseEvent event) {
//
//
//				if (event.getClickCount() == 2 && event.getButton().name().equals(MouseButton.PRIMARY.name())) {
//					System.out.println("鼠标按键 = " + event.getButton().name());
//					System.out.println("双击事件");
//				}
//			}
//
//		});

		// 键盘按键事件 英文输入法有效
		button.setOnKeyPressed((keyEvent) -> {

			// 添加条件过滤按键
			if (keyEvent.getCode().getName().equals(KeyCode.A.getName())) {
				System.out.println("只有按键a才能进入 = " + keyEvent.getCode().getName());
			}

			System.out.println("按下 = " + keyEvent.getCode().getName());
		});

		// 键盘松开事件
		button.setOnKeyReleased((keyEvent) -> {
			System.out.println("松开 = " + keyEvent.getCode().getName());
		});

		Group group = new Group();
		group.getChildren().add(button);

		Scene scene = new Scene(group);

		primaryStage.setScene(scene);

		primaryStage.setTitle("javafx");
		primaryStage.setWidth(600);
		primaryStage.setHeight(600);
		primaryStage.show();
	}

}
